package cn.tramy.pinxian.wind.eventstore

import cn.tramy.pinxian.common.util.logger
import cn.tramy.pinxian.wind.eventstore.domain.model.DomainEvent
import cn.tramy.pinxian.wind.eventstore.storage.EventStorage
import reactor.core.publisher.Flux

class DefaultEventStore(private val eventStorage: EventStorage) : EventStore {
    companion object {
        private val LOGGER = logger(DefaultEventStore::class.java)
    }

    override fun readEvents(aggregateId: String, aggregateType: Class<Any>): Flux<DomainEvent> =
            this.eventStorage.readEventEntry(aggregateId, aggregateType.name).map { DomainEvent.build(it) }

    override fun appendEvent(event: DomainEvent) {
        LOGGER.debug("append event [$event]")
        this.eventStorage.persistent(event.entry())
    }
}